#include "mytcpsocket.h"
#include <QDebug>

MyTcpSocket::MyTcpSocket() : MsgPackage()
{
}

MyTcpSocket::MyTcpSocket(QTcpSocket * s) : MsgPackage()
{
    init(s);
}

void MyTcpSocket::init(QTcpSocket * s)
{
    socket = s;
    peerAddr = socket->peerAddress().toString();
    peerPort = socket->peerPort();
    peerName = socket->peerName();
    descriptor = socket->socketDescriptor();
}

void MyTcpSocket::send(QByteArray data)
{
    socket->write(mkPkg(data));
    socket->flush();
}

// 重载 == 运算符
bool MyTcpSocket::operator==(const MyTcpSocket &A) {
    return descriptor == A.socket->socketDescriptor() ? true : false;
}

void MyTcpSocket::pkgReady(QByteArray data)
{
    // 向上层发送接收数据包信号
    emit this->sigPkgReady(data, this);
}

QString MyTcpSocket::getPeerAddr()
{
    return peerAddr;
}

QString MyTcpSocket::getPeerName()
{
    return peerName;
}

int MyTcpSocket::getPeerPort()
{
    return peerPort;
}

void MyTcpSocket::setPeerAddr(QString peerAddr)
{
    this->peerAddr = peerAddr;
}

void MyTcpSocket::setPeerName(QString peerName)
{
    this->peerName = peerName;
}

void MyTcpSocket::setPeerPort(int peerPort)
{
    this->peerPort = peerPort;
}

QTcpSocket * MyTcpSocket::getTcpSocket()
{
    return socket;
}

int MyTcpSocket::getDescriptor()
{
    return descriptor;
}

void MyTcpSocket::setDescriptor(int descriptor)
{
    this->descriptor = descriptor;
}

void MyTcpSocket::setDelay(int delay)
{
    this->delay = delay;
}

int MyTcpSocket::getDelay()
{
    return delay;
}
